﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lxwmvc
{
    /// <summary>
    /// 发送消息
    /// </summary>
    class WeiXin_SendMessage
    {
        public static string sendTextMessage(string toUser,string fromUser,string content)
        {
            string temp = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{3}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{2}]]></Content>
</xml>";

            return string.Format(temp, toUser, fromUser, content, WeiXin_Fun.DateTimeToStamp(DateTime.Now));
        }

        /// <summary>
        /// 发送图文消息
        /// </summary>
        /// <param name="toUser"></param>
        /// <param name="fromUser"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string sendNewsMessage(string toUser, string fromUser, List<newsItem> list)
        {
            string temp = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>{3}</ArticleCount>
<Articles>
{4}
</Articles>
</xml> ";

            string temp_item = @"<item>
<Title><![CDATA[{0}]]></Title> 
<Description><![CDATA[{1}]]></Description>
<PicUrl><![CDATA[{2}]]></PicUrl>
<Url><![CDATA[{3}]]></Url>
</item>";

            StringBuilder sb = new StringBuilder();
            list.ForEach((o) => {
                sb.AppendFormat(temp_item, o.Title, o.Description, o.PicUrl, o.Url);
            });

            return string.Format(temp, toUser, fromUser, WeiXin_Fun.DateTimeToStamp(DateTime.Now), list.Count, sb);
        }
    }

    class newsItem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string PicUrl { get; set; }
        public string Url { get; set; }
    }
}
